package com.neuedu.oasystem.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.Employee;
import com.neuedu.oasystem.service.EmployeeService;
import com.neuedu.oasystem.service.impl.EmployeeServiceImpl;

public class EmpSystem {

	private Scanner scanner = null;
	private EmployeeService employeeService;
	Employee employee = null;

	public void init() {
		System.out.println("===================================");
		System.out.println("==========欢迎使用人事管理系统==========");
		System.out.println("===================================");
		System.out.println("         1.用户管理                                                ");
		System.out.println("         2.用户类型管理                                        ");
		System.out.println("         3.部门管理                                               ");
		System.out.println("         4.返回上一级                                           ");
		System.out.println("===================================");
	}

	public void work() {

		scanner = new Scanner(System.in);
		employeeService = new EmployeeServiceImpl();

		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				// 调用用管理模块
				System.out.println("调用用管理模块");
				employeeManager();
				break;
			case "2":
				// 调用用户类型管理模块
				System.out.println("调用用户类型管理模块");
				break;
			case "3":
				System.out.println("调用部门管理模块");
				DepartmentSystem departmentSystem = new DepartmentSystem();
				departmentSystem.work();
				break;
			case "4":
				System.out.println("返回上一级");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}
	}

	private void employeeManager() {

		boolean running = true;
		while (running) {
			System.out.println("===================================");
			System.out.println("                                您选了用户管理模块   ");
			System.out.println("===================================");
			System.out.println("1.新增用户");
			System.out.println("2.修改用户");
			System.out.println("3.用户列表");
			System.out.println("4.查看个人信息");
			System.out.println("5.删除员工");
			System.out.println("6.返回上一级");
			System.out.println("===================================");
			System.out.println("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("新增用户 ");
				employeeAdd();
				break;
			case "2":
				System.out.println("修改用户 ");
				employeeEdit();
				break;
			case "3":
				System.out.println("用户列表");
				employeeList();
				break;
			case "4":
				System.out.println("查看个人信息");
				empFindById();
				break;
			case "5":
				System.out.println("删除员工");
				employeeDelete();
				break;
			case "6":
				System.out.println("返回上一级");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}

		}

	}

	private void employeeAdd() {

		System.out.println("请输入部门ID:          ");
		String deptIdString;
		do {
			try {

				deptIdString = scanner.next();
				if (Integer.valueOf(deptIdString) instanceof Integer) {
					break;
				} else {
					deptIdString = scanner.next();
				}
			} catch (Exception e) {
				System.out.println("您的输入有误，请重新输入：");
			}
		} while (true);
		int deptId = Integer.parseInt(deptIdString);

		System.out.println("请输入角色ID:          ");
		String roleIdString;
		do {
			try {

				roleIdString = scanner.next();
				if (Integer.valueOf(roleIdString) instanceof Integer) {
					break;
				} else {
					roleIdString = scanner.next();
				}
			} catch (Exception e) {
				System.out.println("您的输入有误，请重新输入：");
			}
		} while (true);
		int roleId = Integer.parseInt(roleIdString);

		System.out.println("请输入姓名:         ");

		String empName = scanner.next();
		System.out.println("请输入密码:          ");

		String password = scanner.next();
		System.out.println("请输入薪资:           ");

		double sal = scanner.nextDouble();
		System.out.println("请输入身份证号:     ");

		String idNumber = scanner.next();
		System.out.println("请输入地址:        ");

		String adderss = scanner.next();
		System.out.println("请输入电话号:        ");

		String phoneNumber = scanner.next();
		System.out.println("请输入性别:         ");

		String sex = scanner.next();

		boolean bools = false;
		String email;
		do {
			System.out.println("请输入邮箱:          ");
			email = scanner.next();
			if (email.indexOf("@") == -1 || email.indexOf(".") == -1) {
				System.out.println("邮箱格式错误：没有@或.");
			} else if (email.indexOf("@") != email.lastIndexOf("@") || email.indexOf(".") != email.lastIndexOf(".")) {
				System.out.println("邮箱格式错误：含有多个@或.");
			} else if (email.indexOf("@") > email.indexOf(".")) {
				System.out.println("邮箱格式错误：.在@前面");
			} else if (!checkEmailContent(email)) {
				System.out.println("邮箱格式错误：包含特殊字符");
			} else {
				System.out.println("有效邮箱！");
				bools = true;
			}
		} while (bools == false);

		System.out.println("请输入学历:     ");
		String education = scanner.next();

		Date now = new Date();

		employee = new Employee(0, deptId, roleId, empName, password, sal, idNumber, adderss, phoneNumber, sex, email,
				education, now);
		boolean re = employeeService.add(employee);
		if (re) {
			System.out.println("新增成功！");
		}else {
			System.out.println("新增成功！");
		}
	}

	public void employeeEdit() {
		employeeList();
		int empId = 0;
		empFindById();

		System.out.println("请输入部门ID:  ");
		String deptIdString;
		do {
			try {

				deptIdString = scanner.next();
				if (Integer.valueOf(deptIdString) instanceof Integer) {
					break;
				} else {
					deptIdString = scanner.next();
				}
			} catch (Exception e) {

			}
		} while (true);
		int deptId = Integer.parseInt(deptIdString);

		System.out.println(" 请输入角色ID:           ");
		String roleIdString;
		do {
			try {

				roleIdString = scanner.next();
				if (Integer.valueOf(roleIdString) instanceof Integer) {
					break;
				} else {
					roleIdString = scanner.next();
				}
			} catch (Exception e) {
				System.out.println("您的输入有误，请重新输入：");
			}
		} while (true);
		int roleId = Integer.parseInt(roleIdString);

		System.out.println("请输入姓名:        ");

		String empName = scanner.next();
		System.out.println("请输入新的密码:          ");

		String password = scanner.next();
		System.out.println("请输入新的薪资:         ");

		double sal = scanner.nextDouble();
		System.out.println("请输入身份证号:         ");

		String idNumber = scanner.next();
		System.out.println("请输入新的地址:    ");

		String adderss = scanner.next();
		System.out.println("请输入新的电话号:       ");

		String phoneNumber = scanner.next();
		System.out.println("请输入性别:         ");

		String sex = scanner.next();
			
		boolean bools = false;
		String email;
		do {
			System.out.println(" 请输入新的邮箱:         ");	
			email = scanner.next();
			if (email.indexOf("@") == -1 || email.indexOf(".") == -1) {
				System.out.println("邮箱格式错误：没有@或.");
			} else if (email.indexOf("@") != email.lastIndexOf("@") || email.indexOf(".") != email.lastIndexOf(".")) {
				System.out.println("邮箱格式错误：含有多个@或.");
			} else if (email.indexOf("@") > email.indexOf(".")) {
				System.out.println("邮箱格式错误：.在@前面");
			} else if (!checkEmailContent(email)) {
				System.out.println("邮箱格式错误：包含特殊字符");
			} else {
				System.out.println("有效邮箱！");
				bools = true;
			}
		} while (bools == false);
		System.out.println("请输入新的学历:         ");
		String education = scanner.next();
		Date date = null;
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("请输入新的入职时间（yyyy-MM-dd）:");
		try {
       	String scString = scanner.next();
			date = sFormat.parse(scString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		employee = new Employee(empId, deptId, roleId, empName, password, sal, idNumber, adderss, phoneNumber, sex,
				email, education, date);
		boolean re = employeeService.edit(employee);
		if (re) {
			System.out.println("修改成功！");
		}else {
			System.out.println("修改失败！");
		}
	}

	private void employeeDelete() {
		employeeList();
		System.out.println("请输入员工编号");
		int empId = scanner.nextInt();
		try {
			employeeService.deleteById(empId);
		} catch (Exception e) {
			System.out.println("该员工编号不存在，请重新输入");
		}
		System.out.println("删除成功！");
		employeeList();
	}

	private void empFindById() {
		System.out.println("请输入员工编号");
		int empId = scanner.nextInt();
		Employee employee = null;
		try {
			employee = employeeService.findById(empId);
			System.out.println(employee.getEmpId() + "\t" + employee.getDeptId() + "\t" + employee.getRoleId() + "\t"
					+ employee.getEmpName() + "\t" + employee.getPassword() + "\t" + employee.getSal() + "\t"
					+ employee.getIdNumber() + "\t" + employee.getAdderss() + "\t" + employee.getPhoneNumber() + "\t"
					+ employee.getSex() + "\t" + employee.getEmail() + "\t" + employee.getEducation() + "\t"
					+ employee.getHiredate());
		} catch (NullPointerException e) {
			System.out.println("请输入正确的员工编号");
		}

	}

	private void employeeList() {
		List<Employee> list = employeeService.findAll();
		System.out.println("员工编号\t部门编号\t角色编号\t员工姓名\t密码\t薪资\t身份证编号\t用户名称\t地址\t电话号码\t性别\t邮箱\t学历\t入职日期");
		for (Employee employee : list) {
			System.out.println(employee.getEmpId() + "\t" + employee.getDeptId() + "\t" + employee.getRoleId() + "\t"
					+ employee.getEmpName() + "\t" + employee.getPassword() + "\t" + employee.getSal() + "\t"
					+ employee.getIdNumber() + "\t" + employee.getAdderss() + "\t" + employee.getPhoneNumber() + "\t"
					+ employee.getSex() + "\t" + employee.getEmail() + "\t" + employee.getEducation() + "\t"
					+ employee.getHiredate());
		}
	}

	private static boolean checkEmailContent(String email) {
		for (int i = 0; i < email.length(); i++) {
			char c = email.charAt(i);
			if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '@' || c == '.' || c >= '0' && c <= '9') {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}

}
